﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace MVVMStudy.ViewModels
{
    public class ViewModel
    {
        //Form1的实例设置为View
        Form1 _form1;

        public ViewModel(Form1 form1)
        {
            _form1 = form1;
            /*通过搜索整个页面的Controls，然后对各个控件的事件进行订阅 */
            foreach (Control item in _form1.Controls)
            {
                if (item is Button)
                {
                    (item as Button).Click += new EventHandler(ViewModel_Click);
                }

            }
            //启动窗体
            Application.Run(_form1);
        }
        /// <summary>
        /// 具体的事件处理代码
        /// </summary>
        protected void ViewModel_Click(object sender, EventArgs args)
        {
            string result = string.Empty;
            //具体操作
            foreach (Control item in _form1.Controls)
            {
                if (item is TextBox)
                {
                    result += (item as TextBox).Text;
                }
            }
            foreach (Control item in _form1.Controls)
            {
                /* 显示操作后的结果*/
                if (item is Label && item.TabIndex == 5)
                {
                    (item as Label).Text = result.ToString();
                }
            }
        }
    }
}
